package com.zeng.jiudian.service.impl;

import com.zeng.jiudian.bean.HotelModel;
import com.zeng.jiudian.dao.HotelDao;
import com.zeng.jiudian.dao.impl.HotelDaoImpl;
import com.zeng.jiudian.service.HotelService;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/28 - 9:51
 */
public class HotelServiceImpl implements HotelService
{
    @Override
    public int add(HotelModel hotelModel)
    {
        HotelDao hotelDao = new HotelDaoImpl();

        int count = hotelDao.add(hotelModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public List<HotelModel> findAll(HotelModel hotelModel)
    {
        HotelDao hotelDao = new HotelDaoImpl();

        return hotelDao.findAll(hotelModel);
    }

    @Override
    public int getCount(HotelModel hotelModel)
    {
        HotelDao hotelDao = new HotelDaoImpl();

        return hotelDao.getCount(hotelModel);
    }

    @Override
    public int delById(int id)
    {
        HotelDao hotelDao = new HotelDaoImpl();

        int count = hotelDao.delById(id);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public HotelModel findById(int id)
    {
        HotelDao hotelDao = new HotelDaoImpl();

        return hotelDao.findById(id);
    }

    @Override
    public int update(HotelModel hotelModel)
    {
        HotelDao hotelDao = new HotelDaoImpl();

        if ("".equals(hotelModel.getContent()))
        {
            hotelModel.setContent(null);
        }
        if ("".equals(hotelModel.getAddress()))
        {
            hotelModel.setAddress(null);
        }
        if ("".equals(hotelModel.getPhone()))
        {
            hotelModel.setPhone(null);
        }
        if ("".equals(hotelModel.getFax()))
        {
            hotelModel.setFax(null);
        }
        if ("".equals(hotelModel.getEmail()))
        {
            hotelModel.setEmail(null);
        }
        if ("".equals(hotelModel.getWebsite()))
        {
            hotelModel.setWebsite(null);
        }
        if ("".equals(hotelModel.getReservationPhone()))
        {
            hotelModel.setReservationPhone(null);
        }
        if ("".equals(hotelModel.getQrCode()) || "undefined".equals(hotelModel.getQrCode()))
        {
            hotelModel.setQrCode(null);
        }

        int count = hotelDao.update(hotelModel);
        if (count == 1)
        {
            Jedis jedis = new Jedis();
            jedis.select(0);
            jedis.flushDB();
        }

        return count;
    }

    @Override
    public HotelModel getAll()
    {
        Jedis jedis = new Jedis();
        jedis.select(0);
        HotelModel model = null;
        if (jedis.exists("hotelId"))
        {
            model = new HotelModel();
            model.setId(Integer.parseInt(jedis.get("hotelId")));
            model.setContent(jedis.get("hotelcontent"));
            model.setAddress(jedis.get("hoteladdress"));
            model.setPhone(jedis.get("hotelphone"));
            model.setFax(jedis.get("hotelfax"));
            model.setEmail(jedis.get("hotelemail"));
            model.setWebsite(jedis.get("hotelwebsite"));
            model.setReservationPhone(jedis.get("hotelreservationPhone"));
            model.setQrCode(jedis.get("hotelqrCode"));
            model.setCreateTime(jedis.get("hotelCreateTime"));
            model.setUpdateTime(jedis.get("hotelUpdateTime"));
        }
        else
        {
            HotelDao hotelDao = new HotelDaoImpl();
            model = hotelDao.getAll();
            jedis.set("hotelId", model.getId().toString());
            jedis.set("hotelcontent", model.getContent());
            jedis.set("hoteladdress", model.getAddress());
            jedis.set("hotelphone", model.getPhone());
            jedis.set("hotelfax", model.getFax());
            jedis.set("hotelemail", model.getEmail());
            jedis.set("hotelwebsite", model.getWebsite());
            jedis.set("hotelreservationPhone", model.getReservationPhone());
            jedis.set("hotelqrCode", model.getQrCode());
            jedis.set("hotelCreateTime", model.getCreateTime());
            jedis.set("hotelUpdateTime", model.getUpdateTime());
        }

        return model;
    }
}
